package com.feng.demo.jakartacommons.collections;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.BinaryHeap;
import org.apache.commons.collections.TreeBag;
import org.apache.commons.collections.comparators.NullComparator;
import org.apache.commons.collections.iterators.ArrayIterator;

public class CollectionsDemo{

	public static void main(String args[]){
		test();
	}
	
	/**
	 * jakarta Demo
	 * If you encounter a problem with this demo,please contact me.
	 */
	public static void test(){
		// testing for TreeBag
		System.err.println("===== TreeBag Demo =====");
		Bag tb = new TreeBag();

		tb.add("Lorg of the rings");
		tb.add("Matrix");
		tb.add("T3");

		// since we like Matrix and T3 so much we will add them twice
		tb.add("Matrix");
		tb.add("T3");

		// lets count the number of times we added Matrix in
		System.err.println("No. of times Matrix added: " + tb.getCount("Matrix"));

		// create a unique set of our Bag
		System.err.println("Unique set of our Tree Bag: " + tb.uniqueSet());

		// testing for BinaryHeap
		System.err.println("===== BinaryHeap Demo =====");
		BinaryHeap heap = new BinaryHeap();

		// add integers to it in a random order
		heap.add(new Integer(-1));
		heap.add(new Integer(-10));
		heap.add(new Integer(0));
		heap.add(new Integer(-3));
		heap.add(new Integer(5));

		// what does our heap look like now?
		System.err.println("Before removing an element: " + heap);

		// now remove an integer from it
		heap.remove();

		// after removing
		System.err.println("After removing an element: " + heap);

		// testing for ArrayIterators
		System.err.println("===== ArrayIterator Demo =====");

		// create our array
		String[] myArray = {"Mastercard", "Visa", "American Express"};

		// create our iterator
		ArrayIterator itr = new ArrayIterator(myArray);

		System.err.println("Items in array: ");

		// and iterate over it
		while(itr.hasNext()){

			System.err.println(itr.next());
		}

		// testing for NullComparator
		System.err.println("===== NullComparator Demo ======");

		// create a new heap, but this time use the NullComparator
		// and tell the NullComparator that nulls are not higher than other objects
		BinaryHeap heap2 = new BinaryHeap(new NullComparator(false));

		heap2.add(null);
		heap2.add(new Integer("6"));
		heap2.add(new Integer("-6"));
		heap2.add(null);

		System.err.println("Before removing an element: " + heap2);

		// this will remove one of the nulls
		heap2.remove();

		System.err.println("After removing an element: " + heap2);

	}
}